<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/28
 * Time: 9:28
 */

namespace app\home\controller;

use app\home\model\IntegralLog;
use app\home\model\SignConfig;
use app\home\model\SignLog;
use app\Request;


class Sign
{

    public function signConfig(Request $request)
    {
        $signModel = new SignConfig();
        $list = $signModel->select();
        $count = $signModel->count();
        return success('签到配置', ['list' => $list, 'count' => $count]);
    }

    public function signLog(Request $request)
    {
        $signLogModel = new SignLog();
        $info = $signLogModel->where(['uid' => $request->userInfo['id']])->order('create_time', 'desc')->find();
        return success('签到记录', $info);
    }

    public function signIn(Request $request)
    {
        $yesterdayStart = strtotime('yesterday');;
        $yesterdayEnd = strtotime('yesterday 23:00:00');;
        $signLogModel = new SignLog();
        $todaySign = $signLogModel
            ->where(['uid' => $request->userInfo['id']])
            ->where('create_time', '>=', strtotime('today'))
            ->find();
        if ($todaySign) {
            return success('今日已签到');
        }
        $info = $signLogModel
            ->where(['uid' => $request->userInfo['id']])
            ->where('create_time', '>=', $yesterdayStart)
            ->where('create_time', '<=', $yesterdayEnd)
            ->find();
        if ($info) {
            $signModel = new SignConfig();
            if ($info['frequency'] == 7) {
                $frequency = 0;
            } else {
                $frequency = $info['frequency'] + 1;
            }
            $signInfo = $signModel->where(['frequency' => $frequency+1])->find();
            $data = [
                'uid' => $request->userInfo['id'],
                'create_time' => time(),
                'frequency' => $info['frequency'] + 1,
                'sign_id' => $signInfo['id'],
                'amount' => $signInfo['amount'],
            ];
        } else {
            $signModel = new SignConfig();
            $signInfo = $signModel->where(['frequency' => 1])->find();
            $data = [
                'uid' => $request->userInfo['id'],
                'create_time' => time(),
                'frequency' => 1,
                'sign_id' => $signInfo['id'],
                'amount' => $signInfo['amount'],
            ];
        }
        $userModel = new \app\home\model\User();
        $userModel->where(['id' => $request->userInfo['id']])
            ->inc('integral', $signInfo['amount'])
            ->inc('surplus_integral', $signInfo['amount'])
            ->update();
        $IntegralLogModel = new IntegralLog();
        $signLogModel->insert($data);
        $IntegralLogModel->insert([
            'uid' => $request->userInfo['id'],
            'create_time' => time(),
            'type' => 1,
            'mode_type' => 2,
            'num' => $signInfo['amount'],
            'order_id' => $signLogModel->getLastInsID(),
        ]);

        return success('签到成功');
    }
}